Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
React error "Objects are not valid as a React child (found: [object Promise])"

I'm pretty new to React. I'm trying to create a private route handler with the new release of react-router-dom v6, so I need to verify whether the user is authenticated or not in order to know if return the protected route or redirect the user to the login route.

import { Navigate } from 'react-router-dom';
import { useSelector } from 'react-redux';
import { isLogged } from '../helpers/isLogged';

export const PrivateRoute = async ({children}) => {

  // Check if the user is authenticated 
  const userData = useSelector( store => store.user.object);
  const logged = await isLogged(userData);

  return logged 
  ? children
  : <Navigate to="/login" />;
}

The thing is i'm getting some errors due to this, but mainly the object promise one.

What's going on? Thanks in advance!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can't declare a component async, because an async function returns a Promise, so when you'll use it won't be a component but a Promise object.

If you need to do an async operation you need to use the useEffect Hook :

  export const PrivateRoute = ({ children }) => {
  const [logged, setlLogged] = useState(false);
  useEffect(() => {
    isLogged(userData).then((res) => {
      setIsUserLogged(res);
    });
  }, []);
  // Check if the user is authenticated
  const userData = useSelector((store) => store.user.object);

  return logged ? children : <Navigate to="/login" />;
};

You can also use async/await inside the useEffect hook but it is a bit more tricky, i'll let you dig into that

about 4 years ago · Juan Pablo Isaza Report

0

So, thanks to Hugo could figure it out.

I also used this: React Hook Warnings for async function in useEffect: useEffect function must return a cleanup function or nothing

In a nutshell the solution was using useEffect and using a async function inside, and, while this is running, you could optionally rendering anything else, in my case "waiting...".

import { useEffect, useState } from 'react';
import { Navigate } from 'react-router-dom';
import { useSelector } from 'react-redux';
import { isLogged } from '../helpers/isLogged';

export const PrivateRoute = ({children}) => {

  const userData = useSelector((store) => store.user.object);

  const [logged, setLogged] = useState(false);
  const [checking, setChecking] = useState(true);

  useEffect(() => {
    const checkAuth = async () => {
      
       // Check if the user is authenticated
      isLogged(userData).then((res) => {
        setLogged(res);

        // sets checking variable in false so the component can be rendered.
        setChecking(false);
      });
    }
    checkAuth();
  }, [userData]);
 

  if(checking){
    // Renders 'wait' until isLogged is resolved with the data.
    return (<h1>Wait...</h1>);
  }
  return logged ? children : <Navigate to="/login" />
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!